package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.dao.ScoreDao;
import com.itheima.pojo.Record;
import com.itheima.pojo.Score;
import com.itheima.service.AccountService;
import com.itheima.service.RecordService;
import com.itheima.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    @Autowired
    private RecordService recordService;
    @Autowired
    private ScoreService scoreService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void transfer(int from, int to, double money) {

        //转账逻辑
        accountDao.updateMoney(from,-money);
        //int i=5/0;
        accountDao.updateMoney(to,money);


//        Score score = new Score();
//        score.setId(from);
//        score.setScore(5);
//        scoreService.update(score);

        try {
            //新增积分
            Score score = new Score();
            score.setId(from);
            score.setScore(5);
            scoreService.update(score);
        }catch (Exception e){

        }

        //记录历史
        Record record = new Record();
        record.setFrom(from);
        record.setTo(to);
        record.setTime(new Date());
        record.setMoney(money);
        recordService.save(record);



    }
}
